// Package service internal/service/operation_log_service.go
package service

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"gozrd-admin-api/internal/entity/dto"
	"gozrd-admin-api/internal/repository"
	"gozrd-admin-api/pkg/response"
)

// ISysOperationLogService 操作日志服务接口
type ISysOperationLogService interface {
	GetSysOperationLogList(c *gin.Context, Username, BeginTime, EndTime string, PageSize, PageNum int)
	DeleteSysOperationLogById(c *gin.Context, dto dto.SysOperationLogIdDto)
	BatchDeleteSysOperationLog(c *gin.Context, dto dto.DelSysOperationLogDto)
	CleanSysOperationLog(c *gin.Context)
}

// OperationLogServiceImpl 操作日志服务实现
type OperationLogServiceImpl struct {
	operationLogRepository repository.IOperationLogRepository
	validator              *validator.Validate
}

// NewOperationLogService 创建操作日志服务实例
func NewOperationLogService(operationLogRepo repository.IOperationLogRepository) ISysOperationLogService {
	return &OperationLogServiceImpl{
		operationLogRepository: operationLogRepo,
		validator:              validator.New(),
	}
}

// GetSysOperationLogList 获取操作日志列表
func (s *OperationLogServiceImpl) GetSysOperationLogList(c *gin.Context, username, beginTime, endTime string, pageSize, pageNum int) {
	// 参数校验
	if pageNum < 1 || pageSize < 1 {
		response.Fail(c, response.ErrParams, "分页参数错误")
		return
	}

	// 调用Repository层获取数据
	logs, total, err := s.operationLogRepository.GetOperationLogList(pageNum, pageSize, username, beginTime, endTime)
	if err != nil {
		response.Fail(c, response.ErrDatabase, "获取操作日志列表失败："+err.Error())
		return
	}

	// 构建分页结果
	result := map[string]interface{}{
		"total": total,
		"list":  logs,
	}

	response.Success(c, result)
}

// DeleteSysOperationLogById 删除单条操作日志
func (s *OperationLogServiceImpl) DeleteSysOperationLogById(c *gin.Context, dto dto.SysOperationLogIdDto) {
	// 验证DTO
	if err := s.validator.Struct(dto); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	// 调用Repository层
	err := s.operationLogRepository.DeleteOperationLogById(dto.Id)
	if err != nil {
		response.Fail(c, response.ErrDatabase, "删除操作日志失败："+err.Error())
		return
	}

	response.SuccessWithMessage(c, "删除成功", nil)
}

// BatchDeleteSysOperationLog 批量删除操作日志
func (s *OperationLogServiceImpl) BatchDeleteSysOperationLog(c *gin.Context, dto dto.DelSysOperationLogDto) {
	// 验证DTO
	if err := s.validator.Struct(dto); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	// 调用Repository层
	err := s.operationLogRepository.BatchDeleteOperationLog(dto.Ids)
	if err != nil {
		response.Fail(c, response.ErrDatabase, "批量删除操作日志失败："+err.Error())
		return
	}

	response.SuccessWithMessage(c, "批量删除成功", nil)
}

// CleanSysOperationLog 清空操作日志
func (s *OperationLogServiceImpl) CleanSysOperationLog(c *gin.Context) {
	// 调用Repository层
	err := s.operationLogRepository.CleanOperationLog()
	if err != nil {
		response.Fail(c, response.ErrDatabase, "清空操作日志失败："+err.Error())
		return
	}

	response.SuccessWithMessage(c, "清空成功", nil)
}
